package classes.pack;

import java.util.Vector;

import classes.abstr.ExtInterface;

import com.google.android.maps.GeoPoint;


import park.pack.R;
import park.pack.ShowMap;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Gui //extends Activity
{
	private static Vector <ExtInterface> ExtInterfaces = new Vector<ExtInterface>();
	private static Profiling profiling;	
	private static ParkingZones parkingZones;
	private static UIMain uiMain;
	
	
    public Gui(UIMain uiMain) 
    {    	
		//Profile CurrentProfile = GetCurrentProfile();
			
		//!!!! the startup manager should be created before the layout is shown !!!!
		//StartupManager startupManager = new StartupManager(this);	//creates all classes, and loads all content
		
		
		//create and show Gui layout
		//setContentView(R.layout.main);
		
		
		//set listeners
		//SetOnClickListeners();    
		//showStatusPopUp("TEST EK LKGLADF");//test message
		//showPopup(PopUpType.PARKING_STARTED);//testing pop up messages
    }
	
	/////////////////////
	////// GETTERS //////
	
    public static void setUiMain(UIMain UiMain)
    {
    	Gui.uiMain = UiMain;
    }
    
    public static UIMain getUiMain()
    {
    	return Gui.uiMain;
    }
    
	public static Profiling getProfiling()
	{
		return Gui.profiling;
	}
	
	public static ParkingZones getParkingZones()
	{
		return Gui.parkingZones;
	}
	
	public static ExtInterface getExtInterfaceByType(ExtInterfaceType type)
	{
		//loop through interfaces
		for(int i = 0; i < ExtInterfaces.size(); i++)
		{
			if(ExtInterfaces.get(i).getExtInterfaceType() == type)
			{
				return ExtInterfaces.get(i);
			}
		}		
		return null;		
	}
	////// GETTERS //////
	/////////////////////
		
	/////////////////////
	////// SETTERS //////
	
	public static void setProfiling(Profiling profiling)
	{
		Gui.profiling = profiling;
	}
	
	public static void setParkingZones(ParkingZones parkingZones)
	{
		Gui.parkingZones = parkingZones;
	}
	
	////// SETTERS //////
	/////////////////////
	
	public static void showStatusPopUp(PopUpType popUpType)
	{
		String popUpText = "";
		//for each type make a case inside the switch
		switch(popUpType)
		{
			case PARKING_STARTED:
				popUpText = "Started payment for parking service.";
				break;
			case PARKING_STOPPED:
				popUpText = "Stopped payment for parking service.";
				break;
			case FREE_PARKING:
				popUpText = "Started parking for free.";
				break;
			case ALREADY_PARKING:
				popUpText = "You are already parking.";
				break;
			case ALREADY_NOT_PARKING:
				popUpText = "You are already not parking.";
				break;
			default:
				popUpText = "This message should never be read";
				break;
		}
		AlertDialog.Builder dialog = new AlertDialog.Builder(uiMain);
		dialog.setTitle("Parking app");
		dialog.setMessage(popUpText);
		dialog.show();
		
	}
	
	public static void showErrorPopup(ErrorType errorType)
	{
		String popUpText = "";
		//for each type make a case inside the switch
		switch(errorType)
		{
			case ERR_5_4_1_ERR_INTERFACE_TYPE_UNKNOWN:
				popUpText = "ERROR: Interface type is unknown.";
				break;
			case ERR_5_2_1_NO_VALID_PREFERRED_INTERFACE:
				popUpText = "ERROR: No valid interface selected.";
				break;
			default:
				popUpText = "This message should never be read";
				break;
		}
		AlertDialog.Builder dialog = new AlertDialog.Builder(uiMain);
		dialog.setTitle("ERROR");
		dialog.setMessage(popUpText);
		dialog.show();
		
	}
	
	public static void createToast(String message)
	{
		Toast toast = Toast.makeText(uiMain, message, Toast.LENGTH_LONG);
		toast.show();
	}
	public static void showPopUp(String title, String text)
	{
		AlertDialog.Builder dialog = new AlertDialog.Builder(uiMain);
		dialog.setTitle(title);
		dialog.setMessage(text);
		dialog.show();
	}
	
	public static void createExtInterfaces()
	{
		//create all external interfaces
		//ExtInterface newInterface = new ExtInterface(this);
		Gui.ExtInterfaces.add(new TestInterface());		//add test interface
		// not yet implemented
		//this.ExtInterfaces.add(new SmsInterface(this));		//add sms interface
		
	}

	public static void startParkingSequence()
	{

		AlertDialog.Builder builder = new AlertDialog.Builder(Gui.getUiMain());
    	
    	builder.setMessage("Wil je hier parkeren?")
    	       .setCancelable(false)
    	       .setPositiveButton("Ja", new DialogInterface.OnClickListener() 
    	       {
    	           public void onClick(DialogInterface dialog, int id) 
    	           {
    	                
    	        		AppStatus status = Logging.getLatestStatus();

    	        		if(status == AppStatus.PARKING)
    	        		{
    	        			Gui.showStatusPopUp(PopUpType.ALREADY_PARKING);
    	        			
    	        		}
    	        		else if(status == AppStatus.NOT_PARKING || status == AppStatus.FREE_PARKING)
    	        		{
    	        			
    	        			LogRecord rec = Logging.getLatestPosition();
    	        			GeoPoint position = rec.getLocation();
    	        			boolean inParkingZone = ParkingZones.inParkingZone(position);

    	        			if(inParkingZone == true)
    	        			{
    	        				
    	        				ExtInterfaceType preferedExtInterface;
    	        				preferedExtInterface= Gui.getProfiling().getPreferedExtinterface();
    	        				ExtInterface extInterface = Gui.getExtInterfaceByType(preferedExtInterface);
    	        				
    	        				if(extInterface != null)
    	        				{	
    	        					Logging.updateStatus(AppStatus.WAITING_FOR_START, position);
    	        					
    	        					uiMain.btnStartParking.setVisibility(View.GONE);//Gone
    	        					uiMain.btnStopParking.setVisibility(View.VISIBLE);//Visible
    	        			    	
    	        					if(preferedExtInterface == ExtInterfaceType.TEST)
    	        					{
    	        						
    	        						extInterface.startParking();
    	        						
    	        					}
    	        					else if(preferedExtInterface == ExtInterfaceType.SMS)
    	        					{
    	        						extInterface.startParking();
    	        					}
    	        					else
    	        					{
    	        						//generate error message: Interface type unknown
    	        						Gui.showErrorPopup(ErrorType.ERR_5_4_1_ERR_INTERFACE_TYPE_UNKNOWN);
    	        					}
    	        					Logging.updateStatus(AppStatus.PARKING, position);
    	        					Gui.showStatusPopUp(PopUpType.PARKING_STARTED);
    	        					
    	        			        
    	        				}
    	        				else
    	        				{
    	        					//generate error message: No valid interface type selected
    	        					Gui.showErrorPopup(ErrorType.ERR_5_2_1_NO_VALID_PREFERRED_INTERFACE);
    	        				}
    	        			}
    	        			else
    	        			{
    	        				uiMain.btnStartParking.setVisibility(View.GONE);//Gone
    	        				uiMain.btnStopParking.setVisibility(View.VISIBLE);//Visible
    	        				
    	        				Logging.updateStatus(AppStatus.FREE_PARKING, position);
    	        				Gui.showStatusPopUp(PopUpType.FREE_PARKING);
    	        			}	
    	        		}            	        	   
    	           }
    	       })
    	       .setNegativeButton("Nee", new DialogInterface.OnClickListener() 
    	       {
    	           public void onClick(DialogInterface dialog, int id) 
    	           {
    	                dialog.cancel();
    	           }
    	       });
    	AlertDialog alert = builder.create();
    	alert.show();
	}
	
	public static void stopParkingSequence()
	{
		AppStatus status = Logging.getLatestStatus();
		LogRecord rec = Logging.getLatestPosition();
		GeoPoint position = rec.getLocation();
		
		if(status == AppStatus.PARKING)
		{	
			ExtInterfaceType preferedExtInterface = Gui.getProfiling().getPreferedExtinterface();
			ExtInterface extInterface = Gui.getExtInterfaceByType(preferedExtInterface);
			
			uiMain.btnStartParking.setVisibility(View.VISIBLE);//Visible
			uiMain.btnStopParking.setVisibility(View.GONE);//Gone
			
			if(extInterface != null)
			{
				Logging.updateStatus(AppStatus.WAITING_FOR_STOP, position);
				
				if(preferedExtInterface == ExtInterfaceType.TEST)
				{
					extInterface.stopParking();
				}
				else if(preferedExtInterface == ExtInterfaceType.SMS)
				{
					extInterface.stopParking();
				}
				else
				{
					//generate error message: Interface type unknown
					Gui.showErrorPopup(ErrorType.ERR_5_4_1_ERR_INTERFACE_TYPE_UNKNOWN);
				}
				Logging.updateStatus(AppStatus.NOT_PARKING, position);
				Gui.showStatusPopUp(PopUpType.PARKING_STOPPED);
			}
			else
			{
				//generate error message: No valid interface type selected
				Gui.showErrorPopup(ErrorType.ERR_5_2_1_NO_VALID_PREFERRED_INTERFACE);
			}
			
		}
		else if(status == AppStatus.NOT_PARKING)
		{
			Gui.showStatusPopUp(PopUpType.ALREADY_NOT_PARKING);
		}
		else if(status == AppStatus.FREE_PARKING)
		{
			uiMain.btnStartParking.setVisibility(View.VISIBLE);//Visible
			uiMain.btnStopParking.setVisibility(View.GONE);//Gone
			
			Logging.updateStatus(AppStatus.NOT_PARKING, position);
			Gui.showStatusPopUp(PopUpType.PARKING_STOPPED);
		}
	
	}
}
